package com.sitech.portal.server.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.PageForm;
import com.sitech.portal.server.dao.IPortalDao;
import com.sitech.portal.server.service.IPortalService;
import com.sitech.portal.system.domain.TblSYS_LOGINMSG;

public class PortalServiceImpl implements IPortalService {

	private IPortalDao portalDao;
	
	public IPortalDao getPortalDao() {
		return portalDao;
	}

	public void setPortalDao(IPortalDao portalDao) {
		this.portalDao = portalDao;
	}

	@Override
	public void deletePORLETById(String porlet_id) {
		// TODO Auto-generated method stub
		String[] ids = porlet_id.split(",");
		for(int i=0;i<ids.length;i++){
			String id = ids[i];
			if(!id.equals(""))
				portalDao.deletePORLETById(id);
		}
	}

	@Override
	public void deleteRoleMapOfPorlet(String PORLET_ID) {
		// TODO Auto-generated method stub
		portalDao.deleteRoleMapOfPorlet(PORLET_ID);
	}

	@Override
	public List findAllLayouts() {
		// TODO Auto-generated method stub
		return portalDao.findAllLayouts();
	}

	@Override
	public List findAllPorlets() {
		// TODO Auto-generated method stub
		return portalDao.findAllPorlets();
	}
	/**
	 * 获取用户所有角色的porlet
	 */
	public List findUserPorlets(String login_id){
		return portalDao.findUserPorlets(login_id);
	}

	@Override
	public Map findLayoutById(String layout_id) {
		// TODO Auto-generated method stub
		return portalDao.findLayoutById(layout_id);
	}

	@Override
	public Map findPorletById(String porlet_id) {
		// TODO Auto-generated method stub
		return portalDao.findPorletById(porlet_id);
	}

	@Override
	public List findPorletsByROLE_ID(List<Map> ROLE_IDs) {
		// TODO Auto-generated method stub
		return portalDao.findPorletsByROLE_ID(ROLE_IDs);
	}

	@Override
	public Map findUserSetting(String LOGIN_ID) {
		// TODO Auto-generated method stub
		return portalDao.findUserSetting(LOGIN_ID);
	}

	@Override
	public void insertPORLET(Map porlet) {
		// TODO Auto-generated method stub
		portalDao.insertPORLET(porlet);
	}

	@Override
	public void insertRoleMapOfPorlet(String PORLET_ID, String ROLE_ID) {
		// TODO Auto-generated method stub
		portalDao.insertRoleMapOfPorlet(PORLET_ID, ROLE_ID);
	}

	@Override
	public void updatePORLET(Map porlet) {
		// TODO Auto-generated method stub
		portalDao.updatePORLET(porlet);
	}

	@Override
	public void updateRoleMapOfPorlet(String PORLET_ID, String[] ROLE_ID) {
		// TODO Auto-generated method stub
		portalDao.updateRoleMapOfPorlet(PORLET_ID, ROLE_ID);
	}

	@Override
	public void updateUserSetting_LAYOUT(String LOGIN_ID, String LAYOUT_ID) {
		// TODO Auto-generated method stub
		HashMap map = (HashMap)this.findUserSetting(LOGIN_ID);
		if(map==null){
			map = new HashMap();
			map.put("LOGIN_ID", LOGIN_ID);
			map.put("LAYOUT_ID", LAYOUT_ID);
			portalDao.insertUserSetting(map);
		}else{
			portalDao.updateUserSetting_LAYOUT(LOGIN_ID, LAYOUT_ID);
		}
	}

	@Override
	public int updateUserSetting_PORLETS(String LOGIN_ID, String PORLETS) {
		// TODO Auto-generated method stub
		return portalDao.updateUserSetting_PORLETS(LOGIN_ID, PORLETS);
	}

	@Override
	public String loadHelperByHelpId(String helpId) {
		return portalDao.findPortalHelper(helpId);
	}

	@Override
	public void updateHelpeInfo(String helpId, String helpMsg) {
		String msg = this.loadHelperByHelpId(helpId);
		if(msg == null){
			portalDao.savePortalHelper(helpId, helpMsg);
		}else{
			portalDao.updatePortalHelper(helpId, helpMsg);
		}
		
	}

	@Override
	public IPage findAllPorletsList(PageForm pageForm) {
		// TODO Auto-generated method stub
		return portalDao.findAllPorletsList(pageForm);
	}
	
	/**
	 * 获取用户panel
	 * @param user
	 * @return
	 */
	public List findPorletsByUser(TblSYS_LOGINMSG user){
		//获取用户配置的portal
		List list = portalDao.findUserSettingPorlets(user.getLOGIN_ID());
		//如果没有取得个性化配置，则取用户角色的panel
		if(list==null||list.size()==0){
			list = this.findUserPorlets(user.getLOGIN_ID());
		}
		return list;
	}

}
